package com.ruoyi.web.controller.demo.controller;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.web.controller.demo.domain.FileInfo;

/**
 * @ClassName: DemoUploadifyController
 * @Description: 
 * @Author 刘仁
 * @DateTime 2019年10月15日 上午10:49:06
 */
@Controller
@RequestMapping("/upload")
public class DemoUploadifyController {

	/**
     * 搜索相关
     */
    @GetMapping("/upload")
    public String upload()
    {
        return "demo/table/demo/demo";
    }
    
    /**
     * @Title: upload
     * @Description: 可以修改返回值，比如修改上传所有文件的路径。甚至可以直接返回FileInfo实体类信息给前端
     * @Author 刘仁
     * @DateTime 2019年10月15日 下午12:05:29
     * @param request
     * @param rep
     * @param fileInfo
     */
	@PostMapping("/file")
    @ResponseBody
	public void upload(HttpServletRequest request,HttpServletResponse rep, FileInfo fileInfo) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Iterator<String> fileNames = multipartRequest.getFileNames();
		MultipartFile multipartFile = multipartRequest.getFile(fileNames.next());
		/*打印上传文件的原始文件名称*/
		System.out.println("==========>" + multipartFile.getOriginalFilename());
		// 上传文件路径
		String filePath = Global.getUploadPath();
		// 上传并返回新文件名称
		String fileName;
		try {
			fileName = FileUploadUtils.upload(filePath, multipartFile);
			fileInfo.setFileId("1");
			fileInfo.setFilePath(fileName);
			fileInfo.setFileName(multipartFile.getOriginalFilename());
			/*把上传的文件信息插入到数据库中*/
			//fileInfoService.insertFileInfo(fileInfo);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return;
	}
}
